<?php
Yii::import('application.models.geo.GeoActiveRecord');

/**
 * This is the model class for table "city".
 *
 * The followings are the available columns in table 'city':
 * @property string $id
 * @property string $name
 * @property string $alias
 * @property string $latlng
 * @property string $index
 * @property string $group
 *
 * The followings are the available model relations:
 * @property District[] $districts
 * @property PublicLocation[] $publicLocations
 */
class City extends GeoActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return City the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'city';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, alias, latlng, index, group', 'required'),
			array('name, alias, latlng', 'length', 'max'=>50),
			array('index', 'length', 'max'=>10),
			array('group', 'length', 'max'=>8),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, alias, latlng, index, group', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'districts' => array(self::HAS_MANY, 'District', 'city_id'),
			'publicLocations' => array(self::HAS_MANY, 'PublicLocation', 'city_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'alias' => 'Alias',
			'latlng' => 'Latlng',
			'index' => 'Index',
			'group' => 'Group',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('latlng',$this->latlng,true);
		$criteria->compare('index',$this->index,true);
		$criteria->compare('group',$this->group,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function getDataAll($cache = true){
        if($cache)  return Yii::app()->cacheData->getCityDataAll();
        
        $cities = $this->with(array(
            'districts' => array(
                'index'=>'alias',   
            )
        ))->findAll(array(
            'index'=>'alias',
            'order' => 't.`index` ASC, `districts`.`index` ASC',
        ));
        
        $data = array();
        foreach($cities as $city){
            $data[$city->alias] = array(
                'id' => $city->id,
                'name' => $city->name,
                'alias' => $city->alias,
                'latlng' => $city->latlng,
                'districts' => array(),
            );
            foreach($city->districts as $district){
                $data[$city->alias]['districts'][$district->alias] = array(
                    'id' => $district->id,
                    'name' => $district->name,
                    'latlng' => $district->latlng,
                    'alias' => $district->alias,
                );   
            } 
        }
        return $data;
    }

              
    public function getData($key = 'id'){
        return CHtml::listData($this->dataAll, $key, 'name');
    }
    
    public function getAliasById($city_id){
        if(!$city_id) return null;
        $data = CHtml::listData($this->dataAll, 'id', 'alias');
        return !empty($data[$city_id]) ? $data[$city_id] : null;
    }
    
    public function getDistrictAliasById($city_alias, $district_id){
        if(!$district_id) return null;
        if(empty($this->dataAll[$city_alias]['districts'])) return null;
        
        $data = CHtml::listData($this->dataAll[$city_alias]['districts'], 'id', 'alias');
        return !empty($data[$district_id]) ? $data[$district_id] : null;
    }
        
    // City::model()->dataRefById
    private $_dataRefById = array(); 
    public function getDataRefById(){
        if($this->_dataRefById) return $this->_dataRefById;
        foreach($this->dataAll as $city){
            $this->_dataRefById[$city['id']] = $city;    
        }
        return $this->_dataRefById;
    }
}